mainApp.factory("empFactory",function(){

	var service={};
	var object;
	service.setEmp=function(data){
		object=data;

	}
	service.getEmp=function(){
		return object;
	}
	service.getListData=function(currentindex){
		var obj={
			"pageIndex": currentindex,
			"pageSize": 6
		}
		var list = "";
		RequestUtils.postJson('../employee/getByStatus/1',obj).done(function (resp) {
			console.log(resp);
			if(resp.resultCode==0){
				console.log(resp.data);
				list=resp.data;
				CommonUtils.buildFailHint("列表获取","成功");
			}else{
				CommonUtils.buildFailHint('列表获取', '失败');
			}
		});

		return list;
	}

	service.getAllByEmpIdOrName=function(employee_id,name){

		console.log(employee_id);
		console.log(name);
		var list = "";
		RequestUtils.getJson('../employee/get/1' + employee_id + '/' + name).done(function (resp) {
			console.log(resp);
			if(resp.resultCode==0){
				list=resp.data;
				CommonUtils.buildFailHint("查询数据","成功");
			}else{
				CommonUtils.buildFailHint('查询数据', '失败');
			}
		});
		console.log(list);
		return list;
	}
	service.getEmpById=function(id){
		var list = "";
		$.ajax({
			url: '../employee/getById/' + id,
			type: 'GET',
			async: 'true',
			cache: 'no-cache',
			dataType: 'json',
			async: false,//同步
			success: function (result) {
				list=result.data;
			}
		})
		console.log(list);
		return list;
	}


	service.addEmp=function(empObj){
		empObj.information=empObj.jobnum+'入职附件.jpg';
		empObj.status='1';
		var names = [];
		console.log("123");
		console.log(empObj);
		names.push({name:empObj.information })


		RequestUtils.postForm('../employee/add',empObj).done(function (resp) {
			if(resp.resultCode==0){
				console.log(resp);
				CommonUtils.buildSuccessHint("add","成功");
				RequestUtils.fileUpload('../employee/loadphoto',['newPhotoFile'], names);

			}else{
				CommonUtils.buildFailHint('add', '失败');
			}
		});


	}


	service.UpdateEmp=function(emp){
        /*emp.information=emp.jobnum+'修改附件.jpg';
        var names = [];
        console.log("123");
        console.log(emp);
        names.push({name:emp.information })*/

		RequestUtils.postForm('../employee/update', emp)
			.done(function (resp) {
				if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint("update","成功");
				}else{
                    CommonUtils.buildFailHint("update","失败");
				}
			});
	};

	//修改密码
    service.UpdateEmpPwd=function(emp){
        console.log(emp);
        RequestUtils.postForm('../employee/updatePWD', emp)
            .done(function (resp) {
                if(resp.resultCode==0){
                    alert("修改成功");
                }else{
                    alert("失败")
                }
            });
    };

	service.getEmpByJobnumOrName=function(jobnum){
        var list="";
        RequestUtils.getJson('../employee/get/'+jobnum+"/null").done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("查询数据","成功");
            }else{
                CommonUtils.buildFailHint('查询数据', '失败');
            }
        });
        return list;
	};

	//查询所有service
	service.selectAll=function(){
		var list="";
		RequestUtils.getJson('../employee/getAll').done(function (resp) {
			console.log(resp);
			if(resp.resultCode==0){
				list=resp.data;
				CommonUtils.buildFailHint("查询数据","成功");
			}else{
				CommonUtils.buildFailHint('查询数据', '失败');
			}
		});
		return list;
	};

	return service;
})

mainApp.controller("empListController",function($scope,$http,$state,overallSituationService,empFactory){
	overallSituationService.loginIntercept();

	$scope.toUpdate=function(id){
		//console.log( $scope.emplist[index]);
        var tab={url:"employee-update",text:"员工详情",id:"employee-update-tab",closeable:1};
        $scope.$emit('addTab', tab);
		//empFactory.setEmp($scope.emplist[index]);

		var empObj=empFactory.getEmpById(id);
		console.log(id);
		console.log(empObj);
		empFactory.setEmp(empObj);
		$state.go('employee-update');
	}
	$scope.Test=function(){
		var tab={url:"employee-add",text:"员工入职",id:"employee-add-tab",closeable:1};
		$scope.$emit('addTab', tab);
	}

	//获取数据 全部的
	$scope.list=empFactory.getListData(1);
	$scope.emplist=$scope.list.items;

	//查询数据 姓名或者工号
	$scope.empWhere='';
	$scope.selectEmpByWhere=function(){
		var name,jobNum;

		if($("#selectEmpJobnum").val()==''||$("#selectEmpJobnum").val()==null){
			jobNum='null'
		}else {
			jobNum=$("#selectEmpJobnum").val();
		}
		if($("#selectEmpName").val()==''||$("#selectEmpName").val()==null){
			name='null';
		}else {
			name=$("#selectEmpName").val();
		}
		$scope.emplist=empFactory.getAllByEmpIdOrName(jobNum,name);

	}


	//翻页
	$scope.NextListData=function(pageIndex){
		//分页后的数据
		$scope.list=empFactory.getListData(pageIndex);
		console.log($scope.list);
		//每一页的数据
		$scope.emplist=$scope.list.items;


	}
})

mainApp.controller("empUpdateController",function($scope,$http,$state,empFactory,departFactory,overallSituationService){
	overallSituationService.loginIntercept();
	var empObj=empFactory.getEmp();
	console.log(empObj);
    $scope.UpdateEmp=empObj;
	//$scope.UpdateEmp=empFactory.getEmpById(empObj.id);
	//console.log($scope.UpdateEmp);
    //在部门处查询的所有部门信息
    $scope.departList=departFactory.selectAll();
	$scope.update=function(emp,departObj){
		var empOb = {
			"id":empObj.id,
			'number': empObj.number,
			'jobnum': emp.jobnum,
			'name': emp.name,
			'depart.id': departObj.id,
			'post': emp.post,
			'phone': emp.phone,
            'status': emp.status,
            'photo': empObj.photo,
			'email': empObj.email,
			'wechat': empObj.wechat,
			'qq': empObj.qq,
			'enclosure1': empObj.enclosure1,
			'enclosure2': empObj.enclosure2,
			'enclosure3': empObj.enclosure3,
			'password': empObj.password,
			'information':empObj.information
		};
		/*empObj.jobnum=emp.jobnum;
		empObj.name=emp.name;
		empObj.phone=emp.phone;
		empObj.post=emp.post;
		empObj.status=emp.status;
		//empObj.depart=null;
        empObj.departId=departObj.id;

        console.log(emp);
        console.log(departObj);
		console.log(emp);*/

        console.log(empOb);
		empFactory.UpdateEmp(empOb);
	}

    //员工结果下拉框
    $scope.results=[
        {name:"离职",value:0},
        {name:"在职",value:1}
    ];
})


mainApp.controller("empAddController",function($scope,$http,empFactory,departFactory,overallSituationService){
	overallSituationService.loginIntercept();
	//在部门处处查询的所有部门信息
	$scope.departList=departFactory.selectAll();
	$scope.depart;
	$scope.addEmpData;

	console.log($scope.departList);
	$scope.addEmp=function(emp,departObj){
		var empObj = {
			'number': emp.number,
			'jobnum': emp.jobnum,
			'name': emp.name,
			'depart.id': departObj.id,
			'post': emp.post,
			'phone': emp.phone,
			'status': emp.status,
			'information':emp.information,
		};
		console.log(empObj);
		//增加
		empFactory.addEmp(empObj);

	}

})